package com.aurora.util;

/**
 * @program: blog-aurora
 * @description: jgit拉取仓库
 * @author: jonk
 * @create: 2023-08-03 14:06
 **/


import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.nio.file.Paths;

@Slf4j
public class JGitUtil {
    /**
     * 代码分支
     */
    final String branch = "master";
    private String gitUrl ;
    /**
     * 本地存储.git路径
     */
    String patch;
    /**
     * 本地代码路径
     */
    String projectPatch;

    private static SnowFlakeUtil snowFlakeUtil = new SnowFlakeUtil();

    public JGitUtil(String projectName, String gitUrl, String gitSpace) {

        this.gitUrl = gitUrl;
        long nextId = snowFlakeUtil.getNextId();
        projectName =projectName+nextId;
        this.patch = Paths.get(gitSpace, projectName, "/.git").toString();
        this.projectPatch = Paths.get(gitSpace, projectName).toString();
    }

    /**
     * 拉取
     *
     * @return
     */
    public String pull() {
        String result;
        Repository repo = null;
        try {
            repo = new FileRepository(new File(patch));
            Git git = new Git(repo);

            log.info("开始重置");
            //重置
            git.reset()
                    .setMode(ResetCommand.ResetType.HARD)
                    .setRef(branch).call();

            log.info("开始拉取");

            //拉取
            git.pull()
                    .setRemote("origin")
                    .setRemoteBranchName("gh-pages")
                    .call();
            result = "拉取成功!";
            log.info(result);
        } catch (Exception e) {
            result = e.getMessage();
        } finally {
            if (repo != null) {
                repo.close();
            }
        }
        return result;
    }

    /**
     * 重置
     *
     * @return
     */
    @RequestMapping("/reset")
    public String reset() {
        String result;

        Repository repo = null;
        try {
            repo = new FileRepository(new File(patch));
            Git git = new Git(repo);
            git.reset().setMode(ResetCommand.ResetType.HARD).setRef(branch).call();
            result = "重置成功!";

        } catch (Exception e) {
            result = e.getMessage();
        } finally {
            if (repo != null) {
                repo.close();
            }
        }
        return result;
    }

    /**
     * 恢复
     */
    @RequestMapping("/revert")
    public String revert() {
        String result;

        Repository repo = null;
        try {
            repo = new FileRepository(new File(patch));
            Git git = new Git(repo);
            git.revert().call();
            result = "恢复成功!";

        } catch (Exception e) {
            result = e.getMessage();
        } finally {
            if (repo != null) {
                repo.close();
            }
        }
        return result;
    }

    /**
     * 克隆
     *
     * @return
     */
    public String clone() {
        try {
            Git.cloneRepository()
                    .setURI(gitUrl)
                    .setDirectory(new File(projectPatch))
                    .call();
        } catch (GitAPIException e) {
            log.error("{} clone err,projectPatch {} ", gitUrl, projectPatch);
            return "拉取代码失败 " + e.getMessage();
        }
        return projectPatch;
    }

    /**
     * 状态
     */
    public void status() {
        File RepoGitDir = new File(patch);
        Repository repo = null;
        try {
            repo = new FileRepository(RepoGitDir.getAbsolutePath());
            Git git = new Git(repo);
            Status status = git.status().call();
            log.info("Git Change: " + status.getChanged());
            log.info("Git Modified: " + status.getModified());
            log.info("Git UncommittedChanges: " + status.getUncommittedChanges());
            log.info("Git Untracked: " + status.getUntracked());
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally {
            if (repo != null) {
                repo.close();
            }
        }
    }
}
